package com.siyao.redvip.ui.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.siyao.redvip.R;
import com.siyao.redvip.bean.Record;
import com.siyao.redvip.image.ImageLoaderOptions;
import com.siyao.redvip.utils.AppUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MeasuredGridViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;// 加载list_item
	private Context mContext;
	private List<String> dataList;

	public MeasuredGridViewAdapter(Context context) {
		this.mContext = context;
		mInflater = LayoutInflater.from(context);
	}

	public void setDataUrl(List<String> dataList) {
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position > 0 ? 0 : position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater
					.inflate(R.layout.item_image_grid_item, null);
			holder.imView = (ImageView) convertView
					.findViewById(R.id.gridview_image_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 图片的信息
		if (dataList.size() > 0) {
				ImageLoader.getInstance().displayImage(dataList.get(position), holder.imView,
						ImageLoaderOptions.getDefaultInstance());
		}
		holder.imView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AppUtils.toImageDetial(mContext, dataList);
			}
		});
		return convertView;
	}

	private static class ViewHolder {
		public ImageView imView;
	}

}
